let time = null
let time_ = null
import store from '../store'
import {
	myRequestWs
} from './request.js';


// store.commit('setAudioCode', {
// 	code: false
// })

//背景音乐播放状态
let playState = false

const clearUp = (res, acceptStatus = true) => {


	return new Promise((resolve, reject) => {
		//自定义处理onMessage方法
		let page = getCurrentPages();
		let currentRoute = page[page.length - 1].route

		// if (path != currentRoute && path) return;

		let data_ = JSON.parse(res.data)
console.log('获取前数据',data_)
  //   if(data_.code==402){
		// 	uni.showToast({
		// 		title: data_.mag,
		// 		icon:'none'
		// 	});
		// 	setTimeout(()=>{
		// 		uni.switchTab({
		// 		url:'/pages/index/index'
		// 	})
		// 	},1500)
			
		
		// }
		if(data_.code==402) return;
		if(data_.code==403) return resolve(data_);
		
		let data = data_.data
		
		//房间信息有误
		if (data.game.id != uni.getStorageSync("gameId")) return;
		let userData = uni.getStorageSync("userData")
		
		//处理整合队伍数据
		if (data.team) {
			data.team.forEach(el => {
				el.people.forEach(els => {
					if (userData.id == els.user_id) {
						data.userPeople = els
						data.userPeople.power = false
						data.userPeople.teamId = el.id
						data.userPeople.cap_zt = els.cap_zt
						if (data.game_config) data.userPeople.type = data.game_config.type;
						if (el.activ_list) {
							el.activ_list.teamList = {
								title: el.title,
								game_id: el.game_id,
								pic: el.pic,
								rank: el.rank,
								id: el.id,
							}
							data.userPeople.activ_list = el.activ_list
						}

					}
				})
				// //bingo
				// if(data.team_list){
				// 	data.team_list.forEach(item=>{
				// 		if(!item.activ_list) return;
				// 		if(el.id==item.id) el.activ_list=item.activ_list
				// 		item.activ_list.teamList={
				// 			title:item.title,
				// 			game_id:item.game_id, 
				// 			pic:item.pic,
				// 			rank:item.rank,
				// 			id:item.id,
				// 		}
				// 		if(data.userPeople.teamId==item.id) data.userPeople.activ_list=item.activ_list
				// 	})
				// }

        //活跃状态的队伍及队长
				if (data.game_chall) {
					data.game_chall.forEach(item => {
						if (item.team_id == el.id) {
							item.teamList = el
							if (data.userPeople && data.userPeople.teamId == item.team_id &&
								data.userPeople.cap_zt == 2) data.userPeople.power = true
						}
					})
				}
			})
		}


		//队伍是否准备
		if (data.game_chall) {
			data.game_chall.forEach(item => {
				if (data.userPeople.teamId == item.team_id) {
					data.userPeople.status = item.status
				}
			})
		}

		if (data.code != 202) {
			audioStop()
		}
		
		//根据后台相关数据 跳转相应活动  *待完善
		let url = ''
		if (data.code == 201) {
			url = 'pages/createActivity/formTeam'
		} else if (data.code == 202) {
			if(data.game_config.status != 1){
				uni.setStorageSync('video_s', '');
			}			
			if (data.confirm_result && data.confirm_result.length) {
				url = 'pages/gameTemplate/preliminarySummary'
			} else if (data.game_config.status == 1) {
				url = 'pages/createActivity/clapSetout'
			} else if (data.game_config.type == 11 || data.game_config.type == 12) {
				//bingo 游戏类目

				if (time_ == null) {	
					//检测是否有队伍提前完成bingo
					time_ = setInterval(() => {
						if(data.code==204) {
							learInterval(time_)
							time_ = null
							} else if ((+data.game_config.time_s + data.game_config.time - Date.parse(new Date())/1000) <= 0) {
							if (data.userPeople.cap_zt!=2) return;
							
							myRequestWs('home/game/game_end_bingo', {
								game_id: data.game.id
							})
							clearInterval(time_)
							time_ = null
						}

					}, 1000)
				}
				if (!data.userPeople.activ_list) {
					url = 'pages/gameTemplate/bingo/bingo'
				} else {
					data.game_config = data.userPeople.activ_list.config
					data.game_config.activ_id = data.userPeople.activ_list.activ_id
					data.userPeople.activ_list.team = data.userPeople.activ_list.teamList
					data.game_chall = [data.userPeople.activ_list]
					data.quest_list = data.userPeople.activ_list.quest_list
		
					switch (data.userPeople.activ_list.config.type) {
						case 1:
							url = 'pages/gameTemplate/timekeeping/timekeeping'
							break;
						case 2:
							url = 'pages/gameTemplate/timekeeping/timekeeping'
							break;
						case 3:
							url = 'pages/gameTemplate/A9'
							break;
						case 4:
							url = 'pages/gameTemplate/A9'
							break;
						case 5:
							url = 'pages/gameTemplate/A9'
							break;
						case 6:
							url = 'pages/gameTemplate/A9'
							break;
						case 7:
							url = 'pages/gameTemplate/relayGame/relayGame'
							break;
						case 8:
							url = 'pages/gameTemplate/A5'
							break;
						case 9:
							break;
						case 10:
							break;
						case 11:
							break;
						case 12:
							break;

						default:
							break;
					}
					if (data.userPeople.activ_list.time == 0) {
						url = 'pages/gameTemplate/bingo/bingo'
					}else{
									audioPlay(data)
					}
				}

			} else if (data.game_config.type == 9 || data.game_config.type == 10) {
				if (data.game_config.game_status == 1) {
					url = 'pages/gameTemplate/A11'
					// audioStop()
				} else if (data.game_config.game_status == 2 || data.game_config.game_status == 3 || data
					.game_config.game_status == 4) {
					url = 'pages/gameTemplate/A12'
					// setTimeout(() => {
					audioPlay(data)
					// }, 5000)

				}


			} else if (data.game_config.status == 2 || data.game_config.game_status) {


				if (data.game_config.game_status == 0 && data.game_config.type == 7) {
					url = 'pages/gameTemplate/A8'
					audioStop()
				} else if (data.game_config.game_status == 0 && (data.game_config.type == 8 || data
						.game_config.type == 10)) {
					url = 'pages/gameTemplate/A8'
					audioStop()
				} else if (data.game_chall[0].time == 0) {
					audioStop()
					url = 'pages/gameTemplate/timekeeping/timekeeping'
					if (data.game_config.type == 7) url = 'pages/gameTemplate/relayGame/relayGame'
					if (data.game_config.type == 3 || data.game_config.type == 4 || data.game_config.type ==
						5 || data.game_config.type == 6) url = 'pages/gameTemplate/A9'

				} else {
					switch (data.game_config.type) {
						case 1:
							url = 'pages/gameTemplate/timekeeping/timekeeping'
							break;
						case 2:
							url = 'pages/gameTemplate/timekeeping/timekeeping'
							break;
						case 3:
							url = 'pages/gameTemplate/A9'
							break;
						case 4:
							url = 'pages/gameTemplate/A9'
							break;
						case 5:
							url = 'pages/gameTemplate/A9'
							break;
						case 6:
							url = 'pages/gameTemplate/A9'
							break;
						case 7:
							url = 'pages/gameTemplate/relayGame/relayGame'
							break;
						case 8:
							url = 'pages/gameTemplate/A5'
							break;
						case 9:
							break;
						case 10:
							break;
						case 11:
							break;
						case 12:
							break;

						default:
							break;
					}


					audioPlay(data)

				}

			} else if (data.game_config.status == 3) {
				url = 'pages/gameTemplate/preliminarySummary'
			}
		} else if (data.code == 203) {
			url = 'pages/gameTemplate/teamRanking'
			uni.setStorageSync('gameDataVideoId', '');
			// 'pages/gameTemplate/preliminarySummary'

		} else if (data.code == 204) {

			// if(data.game_config.type == 11 || data.game_config.type == 12) return
			if(data.game.team_type==1){
				url = 'pages/gameTemplate/bingo/bingo'
			}else{
				url = 'pages/gameTemplate/teamRanking'
			}
			
		} else if (data.code == 205) {
			if (data.game_type != 0) {
				url = 'pages/gameTemplate/decoration' 
				// if(data.userPeople.cap_zt==1){  
				// 	url='pages/gameTemplate/awaitFinish'
				// }

			} else {
				url = 'pages/createActivity/prepare'
			}
		} else if (data.code == 206) {
			url = 'pages/gameTemplate/decodeing'
		} else {

			url = 'pages/gameTemplate/takeBreak'
		}

  
		//前台跳转 禁止后台覆盖 *待完善 
		if (currentRoute == 'pages/gameTemplate/A3' && url ==
			'pages/gameTemplate/timekeeping/timekeeping' && data.game_chall[0].time != 0) return resolve(
			data_);
		if (currentRoute == 'pages/gameTemplate/teamRanking' && url ==
			'pages/gameTemplate/preliminarySummary') return resolve(data_);
		if ((currentRoute == 'pages/gameTemplate/decoration' || currentRoute ==
				'pages/createActivity/prepare' || currentRoute == 'pages/gameTemplate/awaitFinish') && (
				url == 'pages/createActivity/prepare' || url == 'pages/gameTemplate/awaitFinish'))
			return resolve(data_);
		if (currentRoute == 'pages/gameTemplate/awaitFinish' && url == 'pages/gameTemplate/decoration')
			return resolve(data_);
		if ((currentRoute == 'pages/gameTemplate/over/over' || currentRoute ==
				'pages/gameTemplate/over/createPoster') && url == 'pages/gameTemplate/decoration')
			return resolve(data_);
			if ((currentRoute == 'pages/gameTemplate/over/over' || currentRoute ==
					'pages/gameTemplate/over/createPoster'))
				return resolve(data_);
			
		
		if (url !== currentRoute && currentRoute != 'pages/gameTemplate/clue') {
			if (time !== null) {
				clearTimeout(time);
			}
			time = setTimeout(() => {
				uni.redirectTo({
					url: `/${url}`,
					success() {
						resolve(data_)
					}
				});
			}, 200)

		}
		resolve(data_)
	})


}

// 启动相应背景音乐
function audioPlay(data) {
	if (data.game_config.type !== 8 && !playState) {
		let num = 0
		if (store.state.audioCode.src != data.game_config.audio) {
			
			store.state.audioCode.src = data.game_config.audio;
			store.state.audioCode.loop = true
			store.state.audioCode.volume = 0.3
			store.state.audioCode.obeyMuteSwitch = false
			store.state.audioCode.sessionCategory = 'ambient'
		}

		setTimeout(function_name, store.state.audioCodeDelayed.code)


		store.state.audioCode.onPlay(() => {
			playState = true

		});
		store.state.audioCode.onError((err) => {
			// console.log('播放错误',err)
			num++
			if (num > 5) return;
			function_name()
		})
		store.state.audioCode.onStop(() => {
			playState = false
		});

		function function_name() {
			let page = getCurrentPages();
			let currentRoute = page[page.length - 1].route
			console.log(store.state.audioCodeDelayed.code, currentRoute !== 'pages/gameTemplate/A12')
			if (store.state.audioCodeDelayed.code && currentRoute !== 'pages/gameTemplate/A12') return audioStop();

			store.state.audioCode.play()
			// console.log('开始播放音乐',data.game_config.audio)
		}
	}
}

const audioStop = () => {

	store.state.audioCode.stop()
	store.state.audioCode.destroy();
	// store.state.audioCode.src=''
	store.commit('setAudioCodeDelayed', {
		code: 0
	})
	console.log('关闭背景音乐')
}


module.exports = {
	clearUp,
	audioStop
}
